package com.example.a10928.weifriend.ui.personal_center

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.util.Log
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import com.example.a10928.weifriend.util.StatusBarUtil
import com.githang.statusbar.StatusBarCompat
import com.mylhyl.circledialog.CircleDialog
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_with_draw_activity.*
import java.util.regex.Matcher
import java.util.regex.Pattern

class WithDrawActivity : BaseActivity() {

    var value:Double=0.0

    var type:String=""

    override fun getContentView(): Int {
        return R.layout.activity_with_draw_activity
    }

    override fun initView() {
        StatusBarCompat.setStatusBarColor(this, Color.WHITE,true)
        value=intent.getStringExtra("data").toDouble()
        type=intent.getStringExtra(VariableName.TYPE)
        when(type){
            "1"->{

            }
            "2"->{
                tv_hint.text="账户余额将提现到银联"
            }
        }

        tv_total.setText("账户余额：￥"+intent.getStringExtra("data"))

        supportActionBar?.hide()
        tv_confirm.setOnClickListener {
            Log.v("zzw","num "+et.text.toString());
            if(TextUtils.isEmpty(et.text.toString())){
                L.t("请填写数额")
                return@setOnClickListener
            }
            if(!L.isNumeric(et.text.toString())){
                L.t("填写的不是数字")
                return@setOnClickListener
            }
            L.v("zzw",L.getNumberDecimalDigits( et.text.toString().toDouble()).toString())

            if(L.getNumberDecimalDigits( et.text.toString().toDouble()) > 3){
                L.t("小数点最多3个")
                return@setOnClickListener
            }

            if(
                    value>=et.text.toString().toDouble()){
               var  now=value-et.text.toString().toDouble()

                CircleDialog.Builder()
                        .setCanceledOnTouchOutside(false)
                        .setCancelable(false)
                        .setTitle("提现确认")
                        .setText("提现金额  ￥"+et.text.toString()+"\n剩余金额 "+now.toString()+"\n提醒：提现后将在24小时内到账，请注意查收。\n提示：提现手续费5元/笔")
                        .setNegative("放弃提现", null)
                        .setPositive("确认提现") {
                            App.app().bpService.withdraw(App.app().user.userId,et.text.toString(),type)
                                    .compose(RxUtil.io2mainLoad(this))
                                    .subscribe(
                                            Consumer {
                                                L.t("提现成功")
                                                value=now
                                                tv_total.setText("账户余额：￥"+value.toString())
                                            },
                                            Consumer {
                                                RxUtil.errorException(it)
                                            }
                                    )
                        }
                        .configNegative { params ->  params.textColor= Color.RED }
                        .show(getSupportFragmentManager())
            }else{
                L.t("余额不足")
            }

        }
    }

    override fun initData() {
    }





}
